//-----------------------------------------------------------------------
// <copyright file="a_ReviewDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ReviewDAOBase
    {
        #region Common methods
        public virtual a_Review Createa_ReviewFromReader(IDataReader reader)
        {
            a_Review item = new a_Review();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RID"))) item.RID = (int)reader["RID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProdID"))) item.ProdID = (int)reader["ProdID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Reviewer_UserID"))) item.Reviewer_UserID = (int)reader["Reviewer_UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReviewDate"))) item.ReviewDate = (DateTime)reader["ReviewDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Subject"))) item.Subject = (string)reader["Subject"];
                if (!reader.IsDBNull(reader.GetOrdinal("Body"))) item.Body = (string)reader["Body"];
                if (!reader.IsDBNull(reader.GetOrdinal("Rating"))) item.Rating = (byte)reader["Rating"];
                if (!reader.IsDBNull(reader.GetOrdinal("HelpFul_Yes"))) item.HelpFul_Yes = (int)reader["HelpFul_Yes"];
                if (!reader.IsDBNull(reader.GetOrdinal("HelpfullCount"))) item.HelpfullCount = (int)reader["HelpfullCount"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReviewStatus"))) item.ReviewStatus = (bool)reader["ReviewStatus"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Review methods
            
        public virtual void Createa_Review(a_Review a_Review)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Reviews_ins");
                
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Review.ProdID);
                database.AddInParameter(dbCommand, "@Reviewer_UserID", DbType.Int32, a_Review.Reviewer_UserID);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_Review.Name);
                database.AddInParameter(dbCommand, "@ReviewDate", DbType.DateTime, a_Review.ReviewDate);
                database.AddInParameter(dbCommand, "@Subject", DbType.AnsiString, a_Review.Subject);
                database.AddInParameter(dbCommand, "@Body", DbType.AnsiString, a_Review.Body);
                database.AddInParameter(dbCommand, "@Rating", DbType.Byte, a_Review.Rating);
                database.AddInParameter(dbCommand, "@HelpFul_Yes", DbType.Int32, a_Review.HelpFul_Yes);
                database.AddInParameter(dbCommand, "@HelpfullCount", DbType.Int32, a_Review.HelpfullCount);
                database.AddInParameter(dbCommand, "@ReviewStatus", DbType.Boolean, a_Review.ReviewStatus);
                database.AddOutParameter(dbCommand, "@RID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Review.RID = (int)database.GetParameterValue(dbCommand, "@RID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Review methods
        
        public virtual void Updatea_Review(a_Review a_Review)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Reviews_upd");            
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, a_Review.RID);
                database.AddInParameter(dbCommand, "@ProdID", DbType.Int32, a_Review.ProdID);
                database.AddInParameter(dbCommand, "@Reviewer_UserID", DbType.Int32, a_Review.Reviewer_UserID);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_Review.Name);
                database.AddInParameter(dbCommand, "@ReviewDate", DbType.DateTime, a_Review.ReviewDate);
                database.AddInParameter(dbCommand, "@Subject", DbType.AnsiString, a_Review.Subject);
                database.AddInParameter(dbCommand, "@Body", DbType.AnsiString, a_Review.Body);
                database.AddInParameter(dbCommand, "@Rating", DbType.Byte, a_Review.Rating);
                database.AddInParameter(dbCommand, "@HelpFul_Yes", DbType.Int32, a_Review.HelpFul_Yes);
                database.AddInParameter(dbCommand, "@HelpfullCount", DbType.Int32, a_Review.HelpfullCount);
                database.AddInParameter(dbCommand, "@ReviewStatus", DbType.Boolean, a_Review.ReviewStatus);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Review methods
        public virtual void Deletea_Review(int rid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Reviews_del");
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, rid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Review methods
        
        public virtual a_Review Geta_Review(int rid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Reviews_sel");
                
                database.AddInParameter(dbCommand, "@RID", DbType.Int32, rid);
                
                a_Review a_Review =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Review = Createa_ReviewFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Review;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ReviewList methods
        public virtual a_ReviewCollection Geta_ReviewList(a_ReviewColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Reviews_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ReviewCollection a_ReviewCollection = new a_ReviewCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Review a_Review = Createa_ReviewFromReader(reader);
                        a_ReviewCollection.Add(a_Review);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ReviewCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ReviewCollection Geta_ReviewList(a_ReviewColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ReviewList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}